package pointshop;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2014/11/10.
 */
public class Store implements Listener {
    private final Main plugin;
    private final ItemStack delete = new ItemStack(388);
    private final ItemStack delete5 = new ItemStack(388);
    private final ItemStack delete10 = new ItemStack(388);
    private final ItemStack add = new ItemStack(264);
    private final ItemStack add5 = new ItemStack(264);
    private final ItemStack add10 = new ItemStack(264);
    private final Point_system pointAPI = new Point_system();
    int price = 0;
    int xnum = 0;

    public Store(Main plugin) {

        this.plugin = plugin;
        ItemMeta meta = this.delete.getItemMeta();
        meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&4减一个"));
        this.delete.setItemMeta(meta);
        ItemMeta meta5 = this.delete5.getItemMeta();
        meta5.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&4减五个"));
        this.delete5.setItemMeta(meta5);
        ItemMeta meta10 = this.delete10.getItemMeta();
        meta10.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&4减十个"));
        this.delete10.setItemMeta(meta10);
        meta = this.add.getItemMeta();
        meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&b加一个"));
        this.add.setItemMeta(meta);
        meta5 = this.add5.getItemMeta();
        meta5.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&b加五个"));
        this.add5.setItemMeta(meta5);
        meta10 = this.add10.getItemMeta();
        meta10.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&b加十个月"));
        this.add10.setItemMeta(meta10);

    }


    @EventHandler
    public void onInventoryClick(InventoryClickEvent e) {


        if (e.getCurrentItem() != null) {
            if (e.getInventory().getTitle().equals("§2[Pointshop]点券商店")) {

                e.setCancelled(true);

            }
            if ((e.getCurrentItem().getItemMeta() != null) && (e.getInventory().getTitle().equals("§2[Pointshop]点券商店"))) {

                Player p = (Player) e.getWhoClicked();

                File file = new File(Main.plugin.getDataFolder(), "Shop.yml");
                if (!file.exists())
                    return;
                FileConfiguration config = load(file);
                int slot = e.getRawSlot() + 1;
                for (int x = 1; x <= config.getInt("Num"); x++) {
                    int Slot = config.getInt(x + ".Slot");
                    if (slot == Slot) {
                        xnum = x;


                        try {
                            Inventory confirm = Bukkit.createInventory(null, 9, "§2[Pointshop]请选择购买的数量");
                            ItemStack buy = new ItemStack(2);
                            ItemMeta meta = buy.getItemMeta();
                            meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&a确定购买"));
                            List lore = new ArrayList();
                            lore.add(ChatColor.translateAlternateColorCodes('&', "&2" + "&8: "));
                            lore.add(ChatColor.translateAlternateColorCodes('&', "&2购买时长: 1个月"));
                            lore.add(ChatColor.translateAlternateColorCodes('&', "&2总价: " + "点券"));
                            meta.setLore(lore);
                            buy.setItemMeta(meta);
                            confirm.setItem(0, this.delete);
                            confirm.setItem(1, this.delete5);
                            confirm.setItem(2, this.delete10);
                            confirm.setItem(8, this.add);
                            confirm.setItem(7, this.add5);
                            confirm.setItem(6, this.add10);
                            confirm.setItem(4, buy);
                            e.getWhoClicked().openInventory(confirm);
                        } catch (NullPointerException ex) {
                        }


                    }
                }
            }
            if (e.getInventory().getTitle().equals("§2[Pointshop]请选择购买的数量")) {

                e.setCancelled(true);

            }
            if ((e.getInventory().getTitle().equals("§2[Pointshop]请选择购买的数量")) &&
                    (e.getCurrentItem().getItemMeta() != null)) {
                Player player = (Player) e.getWhoClicked();
                File file = new File(Main.plugin.getDataFolder(), "Shop.yml");
                if (!file.exists())
                    return;
                FileConfiguration config = load(file);
                try {

                    ItemStack i = config.getItemStack(xnum + ".ItemStack");
                    price = config.getInt(xnum + ".Price");
                    String name = config.getString(xnum + ".Name");
                    ItemStack item = e.getInventory().getItem(4);
                    ItemMeta meta = item.getItemMeta();
                    String type = e.getCurrentItem().getItemMeta().getDisplayName();
                    List lore = meta.getLore();
                    if (type.equals(ChatColor.translateAlternateColorCodes('&', "&a确定购买"))) {
                        player.closeInventory();
                        int bnum = Integer.parseInt(((String) lore.get(1)).split("时长: ")[1].split("个月")[0]);
                        String info = "购买" + name;

                        if (this.pointAPI.payPoints(player.getName(), (bnum * price), info)) {
                            for(int j=0;j<bnum;j++){
                                player.getInventory().addItem(new ItemStack[]{i});
                            }
                            player.sendMessage("§a你成功花费" + bnum * price + "点券购买了" + bnum + "个" + name);

                        } else {

                            player.sendMessage("§c对不起，你的点券不足");

                        }
                    }
                    if ((type.equals(ChatColor.translateAlternateColorCodes('&', "&4减一个"))) &&
                            (item.getAmount() > 1)) {
                        item.setAmount(item.getAmount() - 1);
                        lore.set(1, ChatColor.translateAlternateColorCodes('&', "&2购买数量: " + item.getAmount() + "个"));
             lore.set(2, ChatColor.translateAlternateColorCodes('&', "&2总价: " + price * item.getAmount() + "点券"));
                        meta.setLore(lore);
                        item.setItemMeta(meta);
                    }
                    if ((type.equals(ChatColor.translateAlternateColorCodes('&', "&4减五个"))) &&
                            (item.getAmount() > 5)) {
                        item.setAmount(item.getAmount() - 5);
                        lore.set(1, ChatColor.translateAlternateColorCodes('&', "&2购买数量: " + item.getAmount() + "个"));
             lore.set(2, ChatColor.translateAlternateColorCodes('&', "&2总价: " + price * item.getAmount() + "点券"));
                        meta.setLore(lore);
                        item.setItemMeta(meta);
                    }
                    if ((type.equals(ChatColor.translateAlternateColorCodes('&', "&4减十个"))) &&
                            (item.getAmount() > 10)) {
                        item.setAmount(item.getAmount() - 10);
                        lore.set(1, ChatColor.translateAlternateColorCodes('&', "&2购买: " + item.getAmount() + "个"));
             lore.set(2, ChatColor.translateAlternateColorCodes('&', "&2总价: " + price * item.getAmount() + "点券"));
                        meta.setLore(lore);
                        item.setItemMeta(meta);
                    }

                    if ((type.equals(ChatColor.translateAlternateColorCodes('&', "&b加一个"))) &&
                            (item.getAmount() < 64)) {
                        item.setAmount(item.getAmount() + 1);
                        lore.set(1, ChatColor.translateAlternateColorCodes('&', "&2购买数量: " + item.getAmount() + "个"));
             lore.set(2, ChatColor.translateAlternateColorCodes('&', "&2总价: " + price * item.getAmount() + "点券"));

                        meta.setLore(lore);
                        item.setItemMeta(meta);
                    }
                    if ((type.equals(ChatColor.translateAlternateColorCodes('&', "&b加五个"))) &&
                            (item.getAmount() < 59)) {
                        item.setAmount(item.getAmount() + 5);
                        lore.set(1, ChatColor.translateAlternateColorCodes('&', "&2购买数量: " + item.getAmount() + "个"));
             lore.set(2, ChatColor.translateAlternateColorCodes('&', "&2总价: " + price * item.getAmount() + "点券"));
                        meta.setLore(lore);
                        item.setItemMeta(meta);
                    }
                    if ((type.equals(ChatColor.translateAlternateColorCodes('&', "&b加十个"))) &&
                            (item.getAmount() < 54)) {
                        item.setAmount(item.getAmount() + 10);
                        lore.set(1, ChatColor.translateAlternateColorCodes('&', "&2购买数量: " + item.getAmount() + "个"));
             lore.set(2, ChatColor.translateAlternateColorCodes('&', "&2总价: " +price * item.getAmount() + "点券"));
                        meta.setLore(lore);
                        item.setItemMeta(meta);
                    }
                } catch (NullPointerException ex) {
                }
            }
        }
    }

    public FileConfiguration load(File file) {

     if (!file.exists()) {
      Main.plugin.saveResource("Shop.yml", false);
     }
        return YamlConfiguration.loadConfiguration(file);
    }

    public void ShopOpen(Player p) {
        File file = new File(Main.plugin.getDataFolder(), "Shop.yml");

        if (file.exists()) {
            FileConfiguration config = load(file);
            int size = getSize(config);
            if (size == 0) {
                p.sendMessage("§9Pointshop §f- §9目前商店里暂无货品");
                return;
            }
            size = getContainer(size);
            Inventory inv = Bukkit.createInventory(null, size, "§2[Pointshop]点券商店");
            int num = config.getInt("Num");
            for (int x = 1; x <= num; x++) {
                ItemStack i = new ItemStack(config.getItemStack(x + ".ItemStack"));
                ItemMeta meta = i.getItemMeta();
                String name = config.getString(x + ".Name");
                meta.setDisplayName(name);
                int price = config.getInt(x + ".Price");
                List lore = new ArrayList();
                String Lore = config.getString(x + ".Lore");
                lore.add("§2单价" + price + "点券");
                lore.add(Lore);
                meta.setLore(lore);
                i = i.clone();
                i.setItemMeta(meta);


                inv.setItem(config.getInt(x + ".Slot") - 1, i);


            }
            p.openInventory(inv);
            return;
        }
        p.sendMessage("§9Pointshop §f- §9目前商店里尚无货品");
    }

    public int getSize(FileConfiguration config) {
        File file = new File(Main.plugin.getDataFolder(), "Shop.yml");
        if (!file.exists()) {
            return 0;
        }
        config = load(file);
        int size = 0;
        for (int x = 1; x <= config.getInt("Num"); x++) {
            int figure = config.getInt(x + ".Slot");
            if (size < figure) {
                size = figure;
            }
        }
        return size;
    }

    public int getContainer(int x) {
        int y = 0;
        switch (x / 10) {
            case 0:
                y = 9;
                break;
            case 1:
                y = 18;
                break;
            case 2:
                y = 27;
                break;
            case 3:
                y = 36;
                break;
            case 4:
                y = 45;
                break;
            case 5:
                y = 54;
                break;
            default:
                break;
        }
        return y;
    }

    public void addItem(Player p, ItemStack i, int Price, String args, int x, int y, String name) {
        if (i.getType() == Material.AIR) {
            p.sendMessage("§9Pointshop §f- §9无法出售空商品");
            return;
        }
        File file = new File(Main.plugin.getDataFolder(), "Shop.yml");

        if (!file.exists()) {
            FileConfiguration config = load(file);
            config.set("Num", Integer.valueOf(1));

            config.set("1.ItemStack", i);
            config.set("1.Name", String.valueOf(name));
            config.set("1.Price", Integer.valueOf(Price));
            config.set("1.Lore", String.valueOf(args));
            config.set("1.Slot", Integer.valueOf((x - 1) * 9 + y));
            p.sendMessage("§9Pointshop §f- §9增加商品成功");
            try {
                config.save(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        FileConfiguration config = load(file);
        int slot = (x - 1) * 9 + y;
        if (isCover(config, slot)) {
            p.sendMessage("§9Pointshop §f- §9该格位置已有商品");
            return;
        }
        int num = config.getInt("Num");
        if (num == 54) {
            p.sendMessage("§9Pointshop §f- §e商店已满,无法再添加");
            return;
        }
        config.set("Num", Integer.valueOf(num + 1));
        config.set(num + 1 + ".ItemStack", i);
        config.set(num + 1 + ".Name", String.valueOf(name));
        config.set(num + 1 + ".Price", Integer.valueOf(Price));
        config.set(num + 1 + ".Lore",  String.valueOf(args));
        config.set(num + 1 + ".Slot", Integer.valueOf(slot));
        try {
            config.save(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        p.sendMessage("§9Pointshop §f- §9增加商品成功");
    }

    public void removeItem(Player p, int x, int y) {
        File file = new File(Main.plugin.getDataFolder(), "Shop.yml");
        if (!file.exists()) return;
        FileConfiguration config = load(file);
        int slot = (x - 1) * 9 + y;
        for (int i = 1; i <= config.getInt("Num"); i++) {
            if (slot == config.getInt(i + ".Slot")) {
                if (i == config.getInt("Num")) {
                    config.set(Integer.toString(i), null);
                    int num = config.getInt("Num");
                    config.set("Num", Integer.valueOf(num - 1));
                    try {
                        config.save(file);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    p.sendMessage("§9Pointshop §f- §2删除商品成功");
                    return;
                }

                int num = config.getInt("Num");
                config.set("Num", Integer.valueOf(num - 1));
                ItemStack item = config.getItemStack(num + ".ItemStack");
                String name = config.getString(num + ".Name");
                int price = config.getInt(num + ".Price");
                String lore = config.getString(num + ".Lore");
                config.set(i + ".ItemStack", item);
                config.set(i + ".Name", name);
                config.set(i + ".Price", Integer.valueOf(price));
                config.set(i + "Lore", lore);
                try {
                    config.save(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                p.sendMessage("§9Pointshop §f- §2删除商品成功");
                return;
            }
        }

        p.sendMessage("§9Pointshop §f- §e要删除的商品不存在");
    }

    public boolean isCover(FileConfiguration config, int slot) {
        int num = config.getInt("Num");

        for (int abc = 1; abc <= num; abc++) {
            int theSlot = config.getInt(abc + ".Slot");
            if (slot == theSlot) return true;
        }
        return false;
    }


}
